﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace BookShelf.Model.Repositories
{
	public class BookRepository : IBookRepository
	{

		public Book Get(string isbn)
		{

			Book book = null;
			string query = @"SELECT ID, Isbn, Title, PublicationDate, Pages
							FROM Books
							WHERE isbn = @isbn AND Active = 1";
			Database database = DatabaseFactory.CreateDatabase();
			DbCommand command = database.GetSqlStringCommand(query);

			database.AddInParameter(command, "isbn", DbType.String, isbn);

			using (IDataReader reader = command.ExecuteReader())
			{
				try
				{
					while (reader.Read())
					{
						book = new Book((int)reader["ID"],
										reader["isbn"].ToString(),
										reader["title"].ToString(),
										(DateTime)reader["PublicationDate"],
										(int)reader["Pages"]);
					}
				}
				catch (DbException ex)
				{
					//TODO Log Exception
				}

			}

			if (command != null)
				command.Dispose();

			return book;


		}

		public IList<Book> GetAll()
		{
			throw new NotImplementedException();
		}

		public void Add(Book book)
		{
			throw new NotImplementedException();
		}

		public void Remove(Book book)
		{
			throw new NotImplementedException();
		}

	}
}
